package org.apache.servicecomb.samples.practise.houserush.login.api;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.login.aggregate.User;
import org.apache.servicecomb.samples.practise.houserush.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: Snake Man
 * @system:
 * @DateTime: 2022/1/2 1:51
 * @Description:
 */
@RestSchema(schemaId = "userApiRest")
@RequestMapping("/")
public class UserApiRestImpl implements UserApi {
    @Autowired
    private UserService userService;

    /**
     * 创建用户
     * @param user
     * @return
     */
    @PostMapping("users")
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }
    //public User createUser(@RequestBody User user, HttpServletRequest request){
    //    //获取请求头中的token数据
    //    String token = request.getHeader("Authorization");
    //
    //    //对token进行鉴权
    //    int userId = User.verifyTokenGetUserId(token);
    //    //鉴权成功则正常创建用户
    //    return userService.createUser(user);
    //}

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @GetMapping("users/{id}")
    public User findUser(@PathVariable int id) {
        return userService.findUser(id);
    }

    /**
     * 注销
     * @param id
     */
    @DeleteMapping("users/{id}")
    public void removeUser(@PathVariable int id) {
        userService.removeUser(id);
    }

    /**
     * 修改用户密码
     * @param customerId
     * @param user
     * @return
     */
    @PutMapping("users/password")
    public boolean updatePassword(@RequestHeader int customerId,
                                  @RequestBody User user) {
        return userService.updatePassword(customerId, user.getOldPassword(), user.getPassword());
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PutMapping("users/signin")
    public User signin(@RequestBody User user) {
        System.out.println("你好，欢迎登录");
        return userService.signin(user);
    }

    /**
     * 校验token
     * @param token
     * @return
     */
    @PutMapping("users/verify_token")
    public User verifyToken(String token) {
        return userService.verifyToken(token);
    }


}
